package com.longge;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @创建人 xinglongge
 * @创建时间 2021/10/15
 * @描述
 */
public class ContainsNearbyDuplicate {

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])){
                Integer oldIndex = map.get(nums[i]);
                if (i-oldIndex<=k) return true;
            }
            map.put(nums[i],i);
        }
        return false;
    }
}
